<?php

class Bookings extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }

        $this->load->model('booking');
        $this->load->model('user_role');
        $this->load->model('rute');
        $this->load->model('airline');
        $this->load->model('deposit');
        $this->load->model('costumer');
        $this->load->library('user_id');
        $this->load->library('upload');
        $this->load->helper('user_helper');
        $this->load->helper('document');

        if ($this->user_id->cek() == 0) {
            redirect('user_login/user', 'refresh');
        }
    }

    function index() {
        $data['id'] = $this->user_id->id();

        $data['i'] = 1;

        $config = array();
        $config["base_url"] = base_url() . "bookings/index";
        $config["total_rows"] = $this->booking->record_count();
        $config["per_page"] = 15;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->booking->fetch_booking($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
//        $data['rows'] = $this->booking->index();
//        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'bookings/index';
        $this->load->view('template/user', $data);
    }

    function show() {
        $data['id'] = $this->user_id->id();

        $id = $this->uri->segment(3);
        $data['i'] = 1;
        $data['rows'] = $this->booking->show($id);
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'bookings/show';
        $this->load->view('template/user', $data);
    }

    function edit_code() {
        $data['id'] = $this->user_id->id();
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $id_booking = $this->uri->segment(3);
        $data['rows'] = $this->booking->edit($id_booking);
        $data['content'] = 'bookings/edit';
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'admin_bank/new';
        $this->load->view('template/user', $data);
    }

    function create() {
        $bank_name = $this->input->post('bank_name', TRUE);
        $an = $this->input->post('an', TRUE);
        $no_rekening = $this->input->post('no_rekening', TRUE);
        $this->booking->create($bank_name, $an, $no_rekening);
    }

    function edit() {
        $id = $this->uri->segment(3);

        //cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_edit');

        $data['access'] = $access;
        $data['rows'] = $this->booking->edit($id);
        $data['content'] = 'admin_bank/edit';
        $this->load->view('template/user', $data);
    }

    function update() {
        $id = $this->input->post('id', TRUE);
        $bank_name = $this->input->post('bank_name', TRUE);
        $an = $this->input->post('an', TRUE);
        $no_rekening = $this->input->post('no_rekening', TRUE);
        $this->booking->update($id, $bank_name, $an, $no_rekening);
    }

    function delete() {
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_delete');

        $id = $this->uri->segment(3);
        $this->booking->delete($id);
    }

    function code_booking() {

        $config = array(
            'upload_path' => './tiket'
            , 'allowed_types' => 'doc|docx|pdf'
            , 'max_size' => '1000000000'
            , 'max_width' => '2048'
            , 'max_height' => '1024'
        );

        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                array_push($opr, array('error' => $this->upload->display_errors()));
                $title = $this->input->post('title', TRUE);
                $file = $_FILES["userfile"]["name"];
                $id_booking = $this->input->post('id_booking', TRUE);
                $id_user = $this->input->post('id_user', TRUE);
                $fee = $this->input->post('fee', TRUE);
                $code_booking = $this->input->post('code_booking', TRUE);
                $code = $this->input->post('code', TRUE);

                $this->booking->code_booking($id_booking, $id_user, $fee, $total, $code_booking);
            } else {
                //array_push($data,array('error' => 'success'));
                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];

                $id_booking = $this->input->post('id_booking', TRUE);
                $id_user = $this->input->post('id_user', TRUE);
                $fee = $this->input->post('fee', TRUE);
                $price = $this->input->post('price', TRUE);
                $total = $price - $fee;
                $code_booking = $this->input->post('code_booking', TRUE);
                $code = $this->input->post('code', TRUE);
                $file = $_FILES["userfile"]["name"];
                $this->booking->code_booking($id_booking, $id_user, $fee, $total, $code_booking, $pic);
            }
        } else {
            $pic = '';
            echo"c";
        }
        
         $data['id'] = $id_user;
        $data['i'] = 1;
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id_user);

        $data['id'] = $id_user;
        $data['rows'] = $this->costumer->show_book($code, $id_user);
        $data['deposits'] = $this->deposit->index($id_user);
        
        $email_user = $this->costumer->get_email($id_user);
        $message = $this->load->view('costumers/email_issued1', $data, TRUE);
        $this->load->library('email');
        $this->email->set_mailtype("html");
        $this->email->from('stasiuntiketonline.com');

        //email
        
//        $list = array_merge($email, $email_user);
        $this->email->to($email_user);
        $this->email->cc('');
        $this->email->subject('Tiket siap dicetak');
        $this->email->message($message);
        $this->email->send();
        
//        exit;
        
        redirect('bookings/show/' . $id_booking);
    }

    function edit_code_booking() {

        $config = array(
            'upload_path' => './tiket'
            , 'allowed_types' => 'doc|docx|pdf'
            , 'max_size' => '1000000000'
            , 'max_width' => '2048'
            , 'max_height' => '1024'
        );

        $this->upload->initialize($config);

        $pic = '';

        $id_booking = $this->input->post('id_booking', TRUE);
        $id_user = $this->input->post('id_user', TRUE);
        $flight_number = $this->input->post('flight_number', TRUE);
        $price_adult = $this->input->post('price_adult', TRUE);
        $price_child = $this->input->post('price_child', TRUE);
        $price_infant = $this->input->post('price_infant', TRUE);
        $simple_departure_time = $this->input->post('simple_departure_time', TRUE);
        $simple_arrival_time = $this->input->post('simple_arrival_time', TRUE);
        $class = $this->input->post('class', TRUE);
        $fee = $this->input->post('fee', TRUE);
        $price = $this->input->post('price_value', TRUE);
        $total = $price_adult + $price_child + $price_infant - $fee;
        $total_price = $price_adult + $price_child + $price_infant;
        $code = $this->input->post('code', TRUE);
        $code_booking = $this->input->post('code_booking', TRUE);
        $keterangan = $this->input->post('keterangan', TRUE);
        $status = $this->input->post('status', TRUE);
        $file = $this->input->post('file_1');

        if ($status == 0) {
            $status = 1;
        } else {
            $status = $status;
        }
        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
//                array_push($opr, array('error' => $this->upload->display_errors()));
                $this->booking->edit_code_booking($id_booking, $id_user, $flight_number, $price_adult, $price_child, $price_infant, $simple_departure_time, $simple_arrival_time, $class, $fee, $total, $total_price, $code_booking, $keterangan, $status, $file);
            } else {
//                array_push($data,array('error' => 'success'));
                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $file = $_FILES["userfile"]["name"];
                $this->booking->edit_code_booking($id_booking, $id_user, $flight_number, $price_adult, $price_child, $price_infant, $simple_departure_time, $simple_arrival_time, $class, $fee, $total, $total_price, $code_booking, $keterangan, $status, $pic);
            }
        } else {
            $pic = '';
            echo"c";
        }
        
        $data['id'] = $id_user;
        $data['i'] = 1;
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id_user);

        $data['id'] = $id_user;
        $data['rows'] = $this->costumer->show_book($code, $id_user);
        $data['deposits'] = $this->deposit->index($id_user);
        
        $email_user = $this->costumer->get_email($id_user);
        $message = $this->load->view('costumers/email_revisi', $data, TRUE);
        $this->load->library('email');
        $this->email->set_mailtype("html");
        $this->email->from('stasiuntiketonline.com');

        //email
        
//        $list = array_merge($email, $email_user);
        $this->email->to($email_user);
        $this->email->cc('');
        $this->email->subject('Revisi Tiket');
        $this->email->message($message);
        $this->email->send();
        
        redirect('bookings/show/' . $id_booking);
    }

    function download() {
        $url = $this->uri->segment(3);
//        $url = 'cokelat_bendera_a';
//        $name = get_download($url);
        $file = get_file($url);
        $data = file_get_contents("tiket/" . $file);
        $name = $file;
        force_download($name, $data);
    }

}

?>
